package com.quartet.modules.alarm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.quartet.api.ApiResult;
import com.quartet.api.PageResult;
import com.quartet.modules.alarm.dao.TTerminalAlarmInfoDao;
import com.quartet.modules.alarm.dto.TTerminalRealAlarmInfoResponseDto;
import com.quartet.modules.alarm.entity.TTerminalAlarm;
import com.quartet.modules.alarm.service.TTerminalAlarmInfoService;
import com.quartet.modules.customer.dao.TCustomerInfoDao;
import com.quartet.modules.order.service.TOrderService;
import com.quartet.modules.platform.controller.TPlatformStatisticsController;
import com.quartet.modules.platform.dto.*;
import com.quartet.modules.platform.dto.vo.AlarmCountGroupTypeDataVo;
import com.quartet.modules.platform.dto.vo.StatisticsAlarmTypeGroupDataVo;
import com.quartet.modules.statistics.Enum.QueryPeriodEnum;
import com.quartet.modules.statistics.dto.AlarmStatisticsRequestDto;
import com.quartet.modules.statistics.dto.AlarmStatisticsResponseDto;
import com.quartet.modules.system.service.DictDetailServicePs;
import com.quartet.modules.terminal.dao.TTerminalInfoDao;
import com.quartet.modules.vehicle.dao.TDriverInfoDao;
import com.quartet.modules.vehicle.dao.TVehicleInfoDao;
import com.quartet.modules.vehicle.dao.TVehicleOwnerInfoDao;
import com.quartet.modules.vehicle.dto.TVehicleRealAlarmInfoRequestDto;
import com.quartet.utils.BinaryUtil;
import com.quartet.utils.LocaleUtils;
import com.quartet.utils.StringUtils;
import com.quartet.utils.constant.MapServerConstant;
import com.quartet.utils.enums.AlarmTypeEnum;
import com.quartet.utils.enums.PlatformStatisticsEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.persistence.Id;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 其他报警信息表(TOtherAlarmInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-05-18 09:44:34
 */
@Slf4j
@Service("tTerminalAlarmInfoServiceImpl")
public class TTerminalAlarmInfoServiceImpl extends ServiceImpl<TTerminalAlarmInfoDao, TTerminalAlarm> implements TTerminalAlarmInfoService {


    final static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    final static DateTimeFormatter DF_WEEK_MONTH = DateTimeFormatter.ofPattern("yyyy-MM");
    /**
     * 服务对象
     */
    @Resource
    private TVehicleInfoDao tVehicleInfoDao;

    /**
     * 服务对象
     */
    @Resource
    private TVehicleOwnerInfoDao tVehicleOwnerInfoDao;

    /**
     * 服务对象
     */
    @Resource
    private TDriverInfoDao tDriverInfoDao;

    @Autowired
    private TCustomerInfoDao tCustomerInfoDao;

    @Autowired
    private DictDetailServicePs dictDetailServicePs;

    @Autowired
    private TTerminalInfoDao tTerminalInfoDao;

    @Autowired
    private TOrderService tOrderService;

    @Autowired
    private TTerminalAlarmInfoDao tTerminalAlarmInfoDao;

    @Autowired
    private TPlatformStatisticsController tPlatformStatisticsController;


//    /**
//     * 功能描述: 查询其他报警信息
//     *
//     * @param tVehicleAlarmInfoRequestDto
//     * @return java.util.List<com.quartet.modules.alarm.dto.TOtherAlarmInfoResponseDto>
//     * @author huipanxing huipanxing@chinasofti.com
//     * @date 2021/5/18 10:36
//     */
//    @Override
//    public List<TOtherAlarmInfoResponseDto> selectOtherAlarmList(TVehicleAlarmInfoRequestDto tVehicleAlarmInfoRequestDto) {
//        QueryWrapper<TOtherAlarmInfo> queryWrapper = new QueryWrapper<>();
//        if (!ObjectUtils.isEmpty(tVehicleAlarmInfoRequestDto.getVehicleStatus())) {
//            queryWrapper.lambda().eq(TOtherAlarmInfo::getStatus, tVehicleAlarmInfoRequestDto.getVehicleStatus());
//        }
//        List<TOtherAlarmInfo> tFatigueDrivingAlarmInfoList = this.baseMapper.selectList(queryWrapper);
//        List<TOtherAlarmInfoResponseDto> tOtherAlarmInfoResponseDtoList = tFatigueDrivingAlarmInfoList.stream().map(tOtherAlarmInfo -> {
//            TOtherAlarmInfoResponseDto tOtherAlarmInfoResponseDto = BeanUtil.copyProperties(tOtherAlarmInfo, TOtherAlarmInfoResponseDto.class);
//            QueryWrapper<TVehicleInfo> queryWrapper1 = new QueryWrapper<>();
//            queryWrapper1.lambda().eq(TVehicleInfo::getVehicleCode, tOtherAlarmInfo.getVehicleCode());
//            TVehicleInfo tVehicleInfo = tVehicleInfoDao.selectOne(queryWrapper1);
//            if (ObjectUtils.isEmpty(tVehicleInfo)) {
//                return tOtherAlarmInfoResponseDto;
//            }
//            tOtherAlarmInfoResponseDto.setCarNumber(tVehicleInfo.getCarNumber());
//            tOtherAlarmInfoResponseDto.setCarNumberColor(tVehicleInfo.getLicensePlateColor());
//            tOtherAlarmInfoResponseDto.setEquipmentCode(tVehicleInfo.getEquipmentCode());
//            tOtherAlarmInfoResponseDto.setFlowCardNumber(tVehicleInfo.getFlowCardNumber());
//            tOtherAlarmInfoResponseDto.setCustomerCode(tVehicleInfo.getCustomerCode());
//
//            QueryWrapper<TVehicleOwnerInfo> queryWrapper2 = new QueryWrapper();
//            queryWrapper2.lambda().eq(TVehicleOwnerInfo::getVehicleCode, tOtherAlarmInfoResponseDto.getVehicleCode());
//            TVehicleOwnerInfo tVehicleOwnerInfo = tVehicleOwnerInfoDao.selectOne(queryWrapper2);
//            if (ObjectUtils.isEmpty(tVehicleOwnerInfo)) {
//                return tOtherAlarmInfoResponseDto;
//            }
//            tOtherAlarmInfoResponseDto.setOwnersName(tVehicleOwnerInfo.getOwnersName());
//            tOtherAlarmInfoResponseDto.setOwnerPhone(tVehicleOwnerInfo.getOwnerPhone());
//            QueryWrapper<TDriverInfo> queryWrapper3 = new QueryWrapper();
//            queryWrapper3.lambda().eq(TDriverInfo::getVehicleCode, tOtherAlarmInfoResponseDto.getVehicleCode());
//            TDriverInfo tDriverInfo = tDriverInfoDao.selectOne(queryWrapper3);
//            if (ObjectUtils.isNotEmpty(tDriverInfo)) {
//                tOtherAlarmInfoResponseDto.setDriverName(tDriverInfo.getDriverName());
//                tOtherAlarmInfoResponseDto.setDriverPhone(tDriverInfo.getDriverPhone());
//            }
//
//            QueryWrapper<TCustomerInfo> queryWrapper4 = new QueryWrapper<>();
//            queryWrapper4.lambda().eq(TCustomerInfo::getCustomerCode, tOtherAlarmInfoResponseDto.getCustomerCode());
//            TCustomerInfo tCustomerInfo = tCustomerInfoDao.selectOne(queryWrapper4);
//            if (ObjectUtils.isNotEmpty(tCustomerInfo)) {
//                tOtherAlarmInfoResponseDto.setCustomerName(tCustomerInfo.getCustomerName());
//            }
//            return tOtherAlarmInfoResponseDto;
//        }).filter(tOverSpeedAlarmInfoResponseDto -> {
//                    if (CollectionUtils.isEmpty(tVehicleAlarmInfoRequestDto.getCustomerCodeList())) {
//                        return true;
//                    } else {
//                        return tVehicleAlarmInfoRequestDto.getCustomerCodeList().contains(tOverSpeedAlarmInfoResponseDto.getCustomerCode());
//                    }
//                }
//        ).filter(tOtherAlarmInfoResponseDto -> {
//            if (!ObjectUtils.isEmpty(tVehicleAlarmInfoRequestDto.getQueryType()) && (!ObjectUtils.isEmpty(tVehicleAlarmInfoRequestDto.getQueryValue()))) {
//                return ReflectUtil.getFieldValue(tOtherAlarmInfoResponseDto, RandomUtils.getField(tVehicleAlarmInfoRequestDto.getQueryType())).toString().equals(tVehicleAlarmInfoRequestDto.getQueryValue());
//            } else {
//                return true;
//            }
//        }).collect(Collectors.toList());
//        return tOtherAlarmInfoResponseDtoList;
//    }

    /**
     * 查询历史的其他报警信息
     *
     * @param tVehicleRealAlarmInfoRequestDto
     * @return
     */
    @Override
    public ApiResult<PageResult<TTerminalRealAlarmInfoResponseDto>> selectOtherRealAlarmList(TVehicleRealAlarmInfoRequestDto tVehicleRealAlarmInfoRequestDto) {
        Page<TTerminalRealAlarmInfoResponseDto> p = PageHelper.startPage(tVehicleRealAlarmInfoRequestDto.getCurrent(), tVehicleRealAlarmInfoRequestDto.getLimit(), true);
        List<String> alarmTypeCodeList = tVehicleRealAlarmInfoRequestDto.getAlarmTypeCodeList();
        tVehicleRealAlarmInfoRequestDto.setAlarmTypeCodeList(alarmTypeCodeList);
        List<TTerminalRealAlarmInfoResponseDto> tTerminalRealAlarmInfoResponseDtoList = tTerminalAlarmInfoDao.selectOtherRealAlarmList(tVehicleRealAlarmInfoRequestDto);
        tTerminalRealAlarmInfoResponseDtoList.forEach(tTerminalRealAlarmInfoResponseDto -> {
            //将设备时间注入到创建时间中  前端用的创建时间展示的报警时间
            tTerminalRealAlarmInfoResponseDto.setCreateTime(tTerminalRealAlarmInfoResponseDto.getDeviceTime());
            String warningMark = tTerminalRealAlarmInfoResponseDto.getWarningMark();
            List<Integer> alarmType = JSON.parseArray(warningMark, Integer.class);
            List<String> alarmTypeList = new ArrayList<>();
            if (LocaleUtils.getLocale().startsWith(MapServerConstant.EN)) {
                for (Integer integer : alarmType) {
                    alarmTypeList.add(AlarmTypeEnum.find(integer.toString()).getDescriptionEn());
                }
            } else {
                for (Integer integer : alarmType) {
                    alarmTypeList.add(AlarmTypeEnum.find(integer.toString()).getDescription());
                }
            }
            tTerminalRealAlarmInfoResponseDto.setAlarmTypeLabelList(alarmTypeList);
        });
        PageResult<TTerminalRealAlarmInfoResponseDto> pageResult = new PageResult();
        pageResult.setData(tTerminalRealAlarmInfoResponseDtoList);
        pageResult.setTotal(p.getTotal());
        return ApiResult.ok(pageResult);
    }

    /**
     * 获取报警信息
     *
     * @param id
     * @return
     */
    @Override
    public TTerminalRealAlarmInfoResponseDto getAlarmInfoById(Integer id) {
        TTerminalRealAlarmInfoResponseDto tTerminalRealAlarmInfoResponseDto = tTerminalAlarmInfoDao.getAlarmInfoById(id);
        return tTerminalRealAlarmInfoResponseDto;
    }

    /**
     * 获取未读报警消息
     *
     * @return
     */
    @Override
    public List<TTerminalRealAlarmInfoResponseDto> selectTerminalRealAlarmList(List<String> warningMarkList) {
        return tTerminalAlarmInfoDao.selectTerminalRealAlarmList(warningMarkList);
    }

    /**
     * 批量修改告警信息状态
     *
     * @param idList
     */
    @Override
    public void updateTerminalAlarmStatus(List<Integer> idList) {
        tTerminalAlarmInfoDao.updateTerminalAlarmStatus(idList);
    }

    /**
     * 功能描述: 获取过去一周的报警信息
     *
     * @param
     * @return java.util.List<com.quartet.modules.vehicle.dto.TTerminalPositionInfoResponseDto>
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/6/23 17:05
     */
    @Override
    public TPlatformAlarmResponseDto getAlarmInfoLastWeek(List<String> tTerminalCodeList) {
        long start = System.currentTimeMillis();
        TPlatformAlarmResponseDto tPlatformAlarmResponseDto = new TPlatformAlarmResponseDto();
        LocalDateTime currentTime = LocalDateTime.now();
        List<String> axis = new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            String s = df.format(currentTime.minusDays(i));
            axis.add(s);
        }
        tPlatformAlarmResponseDto.setXAxis(axis);
        //获取过去一周的开始时间
        LocalDateTime startTime = currentTime.minusDays(6).withHour(0).withMinute(0).withSecond(0);
        List<Integer> alarmTypeList = MapServerConstant.alarmTypeList;
        List<String> warningMarkList = alarmTypeList.stream().map(s -> String.valueOf(s)).collect(Collectors.toList());

        List<TTerminalAlarmTimeCount> tTerminalAlarmTimeCountList = tTerminalAlarmInfoDao.getAlarmInfoLastWeek(tTerminalCodeList, startTime, warningMarkList);

        List<PlatformAlarmCount> platformAlarmCountList = new ArrayList<>();
        PlatformAlarmCount platformAlarmCount2 = new PlatformAlarmCount();
        platformAlarmCount2.setAlarmTypeLabel(PlatformStatisticsEnum.SPEEDING.getValue());
        LinkedHashMap<String, Integer> map2 = getStringIntegerLinkedHashMap(axis);
        PlatformAlarmCount platformAlarmCount3 = new PlatformAlarmCount();
        platformAlarmCount3.setAlarmTypeLabel(PlatformStatisticsEnum.FATIGUE_DRIVING.getValue());
        Map<String, Integer> map3 = getStringIntegerLinkedHashMap(axis);
        PlatformAlarmCount platformAlarmCount21 = new PlatformAlarmCount();
        platformAlarmCount21.setAlarmTypeLabel(PlatformStatisticsEnum.ENCLOSURE.getValue());
        Map<String, Integer> map21 = getStringIntegerLinkedHashMap(axis);
        PlatformAlarmCount platformAlarmCount24 = new PlatformAlarmCount();
        platformAlarmCount24.setAlarmTypeLabel(PlatformStatisticsEnum.YAW.getValue());
        Map<String, Integer> map24 = getStringIntegerLinkedHashMap(axis);
        for (TTerminalAlarmTimeCount tTerminalAlarmTimeCount : tTerminalAlarmTimeCountList) {

            String warningMark = tTerminalAlarmTimeCount.getWarningMark();
            List<Integer> alarmType = JSON.parseArray(warningMark, Integer.class);
//            List<Integer> integers = BinaryUtil.find1Cursor(Integer.parseInt(warningMark));
            String format = tTerminalAlarmTimeCount.getCreateTime();
            int count = tTerminalAlarmTimeCount.getCount();
            if (alarmType.contains(Integer.parseInt(AlarmTypeEnum.TWO.getValue()))) {
                if (map2.get(format) == null) {
                    map2.put(format, count);
                } else {
                    map2.put(format, map2.get(format) + count);
                }
            }
            if (alarmType.contains(Integer.parseInt(AlarmTypeEnum.THREE.getValue()))) {
                if (map3.get(format) == null) {
                    map3.put(format, count);
                } else {
                    map3.put(format, map3.get(format) + count);
                }
            }
            if (alarmType.contains(Integer.parseInt(AlarmTypeEnum.TWENTY_ONE.getValue()))) {
                if (map21.get(format) == null) {
                    map21.put(format, count);
                } else {
                    map21.put(format, map21.get(format) + count);
                }
            }
            if (alarmType.contains(Integer.parseInt(AlarmTypeEnum.TWENTY_FOUR.getValue()))) {
                if (map24.get(format) == null) {
                    map24.put(format, count);
                } else {
                    map24.put(format, map24.get(format) + count);
                }
            }

        }
        platformAlarmCount2.setCountList(new ArrayList<>(map2.values()));
        platformAlarmCount3.setCountList(new ArrayList<>(map3.values()));
        platformAlarmCount21.setCountList(new ArrayList<>(map21.values()));
        platformAlarmCount24.setCountList(new ArrayList<>(map24.values()));
        platformAlarmCountList.add(platformAlarmCount2);
        platformAlarmCountList.add(platformAlarmCount3);
        platformAlarmCountList.add(platformAlarmCount21);
        platformAlarmCountList.add(platformAlarmCount24);
        tPlatformAlarmResponseDto.setPlatformAlarmCountList(platformAlarmCountList);
        log.info("getAlarmInfoLastWeek time is：{}", System.currentTimeMillis() - start);
        return tPlatformAlarmResponseDto;
    }

    private LinkedHashMap<String, Integer> getStringIntegerLinkedHashMap(List<String> axis) {
        LinkedHashMap<String, Integer> map2 = new LinkedHashMap<>();
        axis.stream().forEach(s -> map2.put(s, 0));
        return map2;
    }

    /**
     * 查询终端近七日在线率
     *
     * @return
     */
    @Override
    public TPlatformTerminalOnLineRateDto getOnlineRateLastWeek(List<String> tTerminalCodeList) {
        long start = System.currentTimeMillis();
        //近七天在线率
        LinkedHashMap<String, String> map1 = new LinkedHashMap<>();
        ////近一年在线率
        LinkedHashMap<String, String> map2 = new LinkedHashMap<>();
        LocalDateTime currentTime = LocalDateTime.now();
        LocalDateTime startTime = currentTime.minusDays(6).withHour(0).withMinute(0).withSecond(0);
        List<String> axisDay = new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            String s = df.format(currentTime.minusDays(i));
            axisDay.add(s);
            map1.put(s, "0");
        }
        LocalDateTime startTime1 = currentTime.minusYears(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        List<String> axisMonth = new ArrayList<>();
        for (int i = 11; i >= 0; i--) {
            String s = DF_WEEK_MONTH.format(currentTime.minusMonths(i));
            axisMonth.add(s);
            map2.put(s, "0");
        }
        //查询近七日分组信息
        List<TTerminalNumberDto> tTerminalOnLineNumberDtoListLastWeek = tTerminalAlarmInfoDao.getOnlineTerminalLastWeek(tTerminalCodeList, startTime, BinaryUtil.convert2Binary(Lists.newArrayList(1)));
        //查询近一年分组信息
        List<TTerminalNumberDto> tTerminalOnLineNumberDtoListLastYear = tTerminalAlarmInfoDao.getOnlineTerminalLastYear(tTerminalCodeList, startTime1, BinaryUtil.convert2Binary(Lists.newArrayList(1)));
//        //查询最近七天的设备
//        List<TTerminalNumberDto> tTerminalNumberDtoListLastWeek = tTerminalInfoDao.getOnlineTerminalLastWeek(startTime);
//        //查询最近一年的设备
//        List<TTerminalNumberDto> tTerminalNumberDtoListLastYear = tTerminalInfoDao.getOnlineTerminalLastYear(startTime1);
        for (TTerminalNumberDto tTerminalNumberDto : tTerminalOnLineNumberDtoListLastWeek) {
            String time = tTerminalNumberDto.getTime();
            int num = tTerminalInfoDao.getOnlineTerminalCurrentDay(time);
            if (num == 0) {
                map1.put(tTerminalNumberDto.getTime(), "0");
            } else {
                map1.put(tTerminalNumberDto.getTime(), tTerminalNumberDto.getCount() * 100 / num + "");
            }

        }

        for (TTerminalNumberDto tTerminalNumberDto : tTerminalOnLineNumberDtoListLastYear) {
            String time = tTerminalNumberDto.getTime();
            int num = tTerminalInfoDao.getOnlineTerminalCurrentMonth(time);
            if (num == 0) {
                map2.put(tTerminalNumberDto.getTime(), "0");
            } else {
                map2.put(tTerminalNumberDto.getTime(), tTerminalNumberDto.getCount() * 100 / num + "");
            }
        }
        TPlatformTerminalOnLineRateDto tPlatformTerminalOnLineRateDto = new TPlatformTerminalOnLineRateDto();

        tPlatformTerminalOnLineRateDto.setDailyAxisList(axisDay);
        tPlatformTerminalOnLineRateDto.setMonthlyAxisList(axisMonth);
        ;
        tPlatformTerminalOnLineRateDto.setDailyOnLineRateList(new ArrayList<>(map1.values()));
        tPlatformTerminalOnLineRateDto.setMonthlyOnLineRateList(new ArrayList<>(map2.values()));
        log.info("getOnlineRateLastWeek time is：{}", System.currentTimeMillis() - start);
        return tPlatformTerminalOnLineRateDto;
    }

    /**
     * 报警统计数据
     *
     * @param alarmStatisticsRequestDto
     * @return
     */
    @Override
    public List<AlarmStatisticsResponseDto> queryAlarmStatisticsData(AlarmStatisticsRequestDto alarmStatisticsRequestDto) {
        List<String> axis=null;
        List<AlarmStatisticsResponseDto> alarmStatisticsResponseDtoList;
        Map<String, List<AlarmStatisticsResponseDto>> map = new HashMap<>();
            if (ObjectUtil.isNotEmpty(alarmStatisticsRequestDto.getStartTime()) && ObjectUtil.isNotEmpty(alarmStatisticsRequestDto.getEndTime())) {
                long days = Duration.between(alarmStatisticsRequestDto.getStartTime(), alarmStatisticsRequestDto.getEndTime()).toDays();
                if (days > 30) {
                    alarmStatisticsResponseDtoList = tTerminalAlarmInfoDao.queryServiceStatisticsDataWithMonth(alarmStatisticsRequestDto);
                    axis = QueryPeriodEnum.getBetweenMonthAxis(alarmStatisticsRequestDto.getStartTime(), alarmStatisticsRequestDto.getEndTime());
                } else {
                    alarmStatisticsResponseDtoList = tTerminalAlarmInfoDao.queryServiceStatisticsDataWithDay(alarmStatisticsRequestDto);
                    axis = QueryPeriodEnum.getBetweenDayAxis(alarmStatisticsRequestDto.getStartTime(), alarmStatisticsRequestDto.getEndTime());
                }
            } else if (QueryPeriodEnum.YESTERDAY.getValue().equals(alarmStatisticsRequestDto.getQueryPeriod()) || QueryPeriodEnum.TODAY.getValue().equals(alarmStatisticsRequestDto.getQueryPeriod())) {
                String queryPeriod = alarmStatisticsRequestDto.getQueryPeriod();
                if (StringUtils.isNotBlank(queryPeriod)) {
                    QueryPeriodEnum queryPeriodEnum = QueryPeriodEnum.find(queryPeriod);
                    queryPeriodEnum.setFilterTime(alarmStatisticsRequestDto);
                    axis = queryPeriodEnum.getAxis();
                }
                alarmStatisticsResponseDtoList = tTerminalAlarmInfoDao.queryServiceStatisticsDataWithHour(alarmStatisticsRequestDto);
            } else {
                String queryPeriod = alarmStatisticsRequestDto.getQueryPeriod();
                if (StringUtils.isNotBlank(queryPeriod)) {
                    QueryPeriodEnum queryPeriodEnum = QueryPeriodEnum.find(queryPeriod);
                    queryPeriodEnum.setFilterTime(alarmStatisticsRequestDto);
                    axis = queryPeriodEnum.getAxis();
                }
                alarmStatisticsResponseDtoList = tTerminalAlarmInfoDao.queryServiceStatisticsDataWithDay(alarmStatisticsRequestDto);
            }
        List<AlarmStatisticsResponseDto> list = new ArrayList<>();
        Map<String, List<AlarmStatisticsResponseDto>> dateTimeListMap = alarmStatisticsResponseDtoList.stream().collect(Collectors.groupingBy(AlarmStatisticsResponseDto::getCreateTime));
        axis.forEach(x -> {
            AlarmStatisticsResponseDto alarmStatisticsResponseDto = new AlarmStatisticsResponseDto();
            alarmStatisticsResponseDto.setCreateTime(x);
            List<AlarmStatisticsResponseDto> alarmStatisticsResponseDtos = dateTimeListMap.get(x);
            if (!CollectionUtils.isEmpty(alarmStatisticsResponseDtos)) {
                int currentTotal = 0;
                for (AlarmStatisticsResponseDto alarmStatisticsResponseDto1 : alarmStatisticsResponseDtos) {
                    Integer count = alarmStatisticsResponseDto1.getCount();
                    String warningMark = alarmStatisticsResponseDto1.getWarningMark();
                    List<Integer> alarmType = JSON.parseArray(warningMark, Integer.class);
                    int size = alarmType.size();
                    currentTotal = currentTotal + count * size;
                }
                alarmStatisticsResponseDto.setCount(currentTotal);
            } else {
                alarmStatisticsResponseDto.setCount(0);
            }
            list.add(alarmStatisticsResponseDto);
        });
        return list;
    }

//    /**
//     * 分页查询报警统计信息
//     * @param alarmStatisticsRequestDto
//     * @return
//     */
//    @Override
//    public PageResult<TTerminalRealAlarmInfoResponseDto> queryAlarmStatisticsList(AlarmStatisticsRequestDto alarmStatisticsRequestDto) {
//        log.info("---------queryAlarmStatisticsList begin, param is {}----------", alarmStatisticsRequestDto);
//        String queryPeriod = alarmStatisticsRequestDto.getQueryPeriod();
//        QueryPeriodEnum queryPeriodEnum = QueryPeriodEnum.find(queryPeriod);
//        queryPeriodEnum.setFilterTime(alarmStatisticsRequestDto);
//        Page<TTerminalRealAlarmInfoResponseDto> p = PageHelper.startPage(alarmStatisticsRequestDto.getCurrent(), alarmStatisticsRequestDto.getLimit(), true);
//        List<String> alarmTypeCodeList = null;
//        if (!CollectionUtils.isEmpty(alarmStatisticsRequestDto.getAlarmTypeCodeList())) {
//            alarmTypeCodeList = alarmStatisticsRequestDto.getAlarmTypeCodeList().stream().map(s -> String.valueOf(BinaryUtil.convert2Binary(Lists.newArrayList(Integer.parseInt(s))))).collect(Collectors.toList());
//        }
//        alarmStatisticsRequestDto.setAlarmTypeCodeList(alarmTypeCodeList);
//        List<TTerminalRealAlarmInfoResponseDto> tTerminalRealAlarmInfoResponseDtoList = this.baseMapper.queryAlarmStatisticsList(alarmStatisticsRequestDto);
//        tTerminalRealAlarmInfoResponseDtoList.forEach(tTerminalRealAlarmInfoResponseDto -> {
//            String warningMark = tTerminalRealAlarmInfoResponseDto.getWarningMark();
//            List<Integer> list = BinaryUtil.find1Cursor(Integer.parseInt(warningMark));
//            List<String> alarmTypeList = new ArrayList<>();
//            if (LocaleUtils.getLocale().startsWith(MapServerConstant.EN)) {
//                for (Integer integer : list) {
//                    alarmTypeList.add(AlarmTypeEnum.find(integer.toString()).getDescriptionEn());
//                }
//            } else {
//                for (Integer integer : list) {
//                    alarmTypeList.add(AlarmTypeEnum.find(integer.toString()).getDescription());
//                }
//            }
//            tTerminalRealAlarmInfoResponseDto.setAlarmTypeLabelList(alarmTypeList);
//        });
//        PageResult<TTerminalRealAlarmInfoResponseDto> pageResult = new PageResult();
//        pageResult.setData(tTerminalRealAlarmInfoResponseDtoList);
//        pageResult.setTotal(p.getTotal());
//        return pageResult;
//    }

    /**
     * 高度关注按时间分组查询报警统计信息
     *
     * @param alarmStatisticsRequestDto
     * @return
     */
    @Override
    public Map<String, List<AlarmStatisticsResponseDto>> queryAlarmStatisticsFollowData(AlarmStatisticsRequestDto alarmStatisticsRequestDto) {
        List<Integer> alarmTypeList = MapServerConstant.alarmTypeList;
        return getStringListMap(alarmStatisticsRequestDto, alarmTypeList);

//        List<AlarmStatisticsResponseDto> list = new ArrayList<>();
//        Map<String, List<AlarmStatisticsResponseDto>> dateTimeListMap = alarmStatisticsResponseDtoList.stream().collect(Collectors.groupingBy(AlarmStatisticsResponseDto::getCreateTime));
//
//        axis.forEach(x -> {
//            Map<Integer, Integer> map = alarmTypeList.stream().collect(Collectors.toMap(s -> s, m -> 0));
//            AlarmStatisticsResponseDto alarmStatisticsResponseDto = new AlarmStatisticsResponseDto();
//            alarmStatisticsResponseDto.setCreateTime(x);
//            List<AlarmStatisticsResponseDto> alarmStatisticsResponseDtos = dateTimeListMap.get(x);
//            if (!CollectionUtils.isEmpty(alarmStatisticsResponseDtos)) {
//                int currentTotal = 0;
//                for (AlarmStatisticsResponseDto alarmStatisticsResponseDto1 : alarmStatisticsResponseDtos) {
//                    Integer count = alarmStatisticsResponseDto1.getCount();
//                    Integer warningMark = alarmStatisticsResponseDto1.getWarningMark();
//                    List<Integer> cursor = BinaryUtil.find1Cursor(warningMark);
//                    currentTotal = currentTotal + count * cursor.size();
//                    for (Integer integer : cursor) {
//                        if (alarmTypeList.contains(integer)){
//                            map.put(integer,map.get(integer)+currentTotal);
//                        }
//                    }
//                }
//                alarmStatisticsResponseDto.setCount(currentTotal);
//            } else {
//                alarmStatisticsResponseDto.setCount(0);
//            }
//            list.add(alarmStatisticsResponseDto);
//        });
//        return null;
    }

    /**
     * 功能描述: 获取报警类型的折线图
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/11/11 20:33
     * @param alarmStatisticsRequestDto
     * @param alarmTypeList
     * @return java.util.Map<java.lang.String, java.util.List < com.quartet.modules.statistics.dto.AlarmStatisticsResponseDto>>
     */
    private Map<String, List<AlarmStatisticsResponseDto>> getStringListMap(AlarmStatisticsRequestDto alarmStatisticsRequestDto, List<Integer> alarmTypeList) {
        List<String> axis=null;
        List<AlarmStatisticsResponseDto> alarmStatisticsResponseDtoList;
        Map<String, List<AlarmStatisticsResponseDto>> map = new HashMap<>();
        for (Integer integer : alarmTypeList) {
            alarmStatisticsRequestDto.setAlarmType(String.valueOf(integer));
            if (ObjectUtil.isNotEmpty(alarmStatisticsRequestDto.getStartTime()) && ObjectUtil.isNotEmpty(alarmStatisticsRequestDto.getEndTime())) {
                long days = Duration.between(alarmStatisticsRequestDto.getStartTime(), alarmStatisticsRequestDto.getEndTime()).toDays();
                if (days > 30) {
                    alarmStatisticsResponseDtoList = tTerminalAlarmInfoDao.queryServiceStatisticsDataWithMonth(alarmStatisticsRequestDto);
                    axis = QueryPeriodEnum.getBetweenMonthAxis(alarmStatisticsRequestDto.getStartTime(), alarmStatisticsRequestDto.getEndTime());
                } else {
                    alarmStatisticsResponseDtoList = tTerminalAlarmInfoDao.queryServiceStatisticsDataWithDay(alarmStatisticsRequestDto);
                    axis = QueryPeriodEnum.getBetweenDayAxis(alarmStatisticsRequestDto.getStartTime(), alarmStatisticsRequestDto.getEndTime());
                }
            } else if (QueryPeriodEnum.YESTERDAY.getValue().equals(alarmStatisticsRequestDto.getQueryPeriod()) || QueryPeriodEnum.TODAY.getValue().equals(alarmStatisticsRequestDto.getQueryPeriod())) {
                String queryPeriod = alarmStatisticsRequestDto.getQueryPeriod();
                if (StringUtils.isNotBlank(queryPeriod)) {
                    QueryPeriodEnum queryPeriodEnum = QueryPeriodEnum.find(queryPeriod);
                    queryPeriodEnum.setFilterTime(alarmStatisticsRequestDto);
                    axis = queryPeriodEnum.getAxis();
                }
                alarmStatisticsResponseDtoList = tTerminalAlarmInfoDao.queryServiceStatisticsDataWithHour(alarmStatisticsRequestDto);
            } else {
                String queryPeriod = alarmStatisticsRequestDto.getQueryPeriod();
                if (StringUtils.isNotBlank(queryPeriod)) {
                    QueryPeriodEnum queryPeriodEnum = QueryPeriodEnum.find(queryPeriod);
                    queryPeriodEnum.setFilterTime(alarmStatisticsRequestDto);
                    axis = queryPeriodEnum.getAxis();
                }
                alarmStatisticsResponseDtoList = tTerminalAlarmInfoDao.queryServiceStatisticsDataWithDay(alarmStatisticsRequestDto);
            }
            List<AlarmStatisticsResponseDto> list = new ArrayList<>();
            Map<String, List<AlarmStatisticsResponseDto>> dateTimeListMap = alarmStatisticsResponseDtoList.stream().collect(Collectors.groupingBy(AlarmStatisticsResponseDto::getCreateTime));
            axis.forEach(x -> {
                AlarmStatisticsResponseDto alarmStatisticsResponseDto = new AlarmStatisticsResponseDto();
                alarmStatisticsResponseDto.setCreateTime(x);
                List<AlarmStatisticsResponseDto> alarmStatisticsResponseDtos = dateTimeListMap.get(x);
                if (!CollectionUtils.isEmpty(alarmStatisticsResponseDtos)) {
                    int currentTotal = 0;
                    for (AlarmStatisticsResponseDto alarmStatisticsResponseDto1 : alarmStatisticsResponseDtos) {
                        Integer count = alarmStatisticsResponseDto1.getCount();
                        String warningMark = alarmStatisticsResponseDto1.getWarningMark();
                        List<Integer> alarmType = JSON.parseArray(warningMark, Integer.class);
                        int size = alarmType.size();
                        currentTotal = currentTotal + count * size;
                    }
                    alarmStatisticsResponseDto.setCount(currentTotal);
                } else {
                    alarmStatisticsResponseDto.setCount(0);
                }
                list.add(alarmStatisticsResponseDto);
            });
            String alarmTypeLabel = dictDetailServicePs.getLabelByValueByCodeAndName(String.valueOf(integer), "alarmType");
            map.put(alarmTypeLabel, list);
            alarmStatisticsRequestDto.setStartTime(null);
            alarmStatisticsRequestDto.setEndTime(null);
        }
        return map;
    }

    /**
     * 功能描述: 根据客户和应用类型以及时间区间统计各种报警类型的数量
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/11/11 11:28
     * @param alarmStatisticsRequestDto
     * @return java.util.List<com.quartet.modules.statistics.dto.AlarmStatisticsGroupTypeResponseDto>
     */
    @Override
    public StatisticsAlarmTypeGroupDataVo queryAlarmStatisticsGroupType(AlarmStatisticsRequestDto alarmStatisticsRequestDto) {
        //设置开始时间
        if (ObjectUtil.isNotEmpty(alarmStatisticsRequestDto.getStartTime()) && ObjectUtil.isNotEmpty(alarmStatisticsRequestDto.getEndTime())) {

        } else if (QueryPeriodEnum.YESTERDAY.getValue().equals(alarmStatisticsRequestDto.getQueryPeriod()) || QueryPeriodEnum.TODAY.getValue().equals(alarmStatisticsRequestDto.getQueryPeriod())) {
            String queryPeriod = alarmStatisticsRequestDto.getQueryPeriod();
            if (StringUtils.isNotBlank(queryPeriod)) {
                QueryPeriodEnum queryPeriodEnum = QueryPeriodEnum.find(queryPeriod);
                queryPeriodEnum.setFilterTime(alarmStatisticsRequestDto);
            }
        } else {
            String queryPeriod = alarmStatisticsRequestDto.getQueryPeriod();
            if (StringUtils.isNotBlank(queryPeriod)) {
                QueryPeriodEnum queryPeriodEnum = QueryPeriodEnum.find(queryPeriod);
                queryPeriodEnum.setFilterTime(alarmStatisticsRequestDto);
            }
        }
        List<AlarmCountGroupTypeDataVo> alarmCountGroupTypeDataVoList = tTerminalAlarmInfoDao.queryAlarmCountGroupType(alarmStatisticsRequestDto);
        StatisticsAlarmTypeGroupDataVo statisticsAlarmTypeGroupDataVo = tPlatformStatisticsController.getStatisticsAlarmTypeGroupDataVo(alarmCountGroupTypeDataVoList);
        return statisticsAlarmTypeGroupDataVo;
    }

    /**
     * 功能描述: 根据客户和应用类型以及时间区间统计最近3、7、30、自定义时间的报警类型折线图
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/11/11 20:21
     * @param alarmStatisticsRequestDto
     * @return java.util.Map<java.lang.String, java.util.List < com.quartet.modules.statistics.dto.AlarmStatisticsResponseDto>>
     */
    @Override
    public Map<String, List<AlarmStatisticsResponseDto>> queryAlarmStatisticsChartGroupTime(AlarmStatisticsRequestDto alarmStatisticsRequestDto) {
        List<Integer> alarmTypeList = MapServerConstant.allAlarmTypeList;
        return getStringListMap(alarmStatisticsRequestDto, alarmTypeList);
    }

//    /**
//     * 统计其他报警的信息
//     *
//     * @param tVehicleRealAlarmInfoRequestDto
//     * @return
//     */
//    @Override
//    public List<TVehicleAlarmInfoStatisticsResponseDto> selectOtherAlarmStatisticsList(TVehicleRealAlarmInfoRequestDto tVehicleRealAlarmInfoRequestDto) {
//        List<TVehicleAlarmInfoStatisticsDto> tVehicleAlarmInfoStatisticsDtoList = this.baseMapper.selectOtherAlarmStatisticsList(tVehicleRealAlarmInfoRequestDto);
//        //组装数据
//        List<TVehicleAlarmInfoStatisticsResponseDto> tVehicleAlarmInfoStatisticsResponseDtoList = tVehicleAlarmInfoStatisticsDtoList.stream().map(tVehicleAlarmInfoStatisticsDto -> {
//            String alarmTypeString = tVehicleAlarmInfoStatisticsDto.getAlarmTypeString();
//            String mileageString = tVehicleAlarmInfoStatisticsDto.getMileageString();
//            String[] alarmTypeSplit = alarmTypeString.split(",");
//            String[] mileageSplit = mileageString.split(",");
//            Map<String, Integer> alarmTypeMap = new HashMap<>();
//            Map<String, Float> mileageMap = new HashMap<>();
//            Integer count;
//            Float totalMileage;
//            for (int i = 0; i < alarmTypeSplit.length; i++) {
//                String alarm = alarmTypeSplit[i];
//                String mileage = mileageSplit[i];
//                count = alarmTypeMap.get(alarm);
//                totalMileage = mileageMap.get(alarm);
//                if (count != null) {
//                    alarmTypeMap.put(alarm, count + 1);
//                    mileageMap.put(alarm, totalMileage + Float.valueOf(mileage));
//                } else {
//                    alarmTypeMap.put(alarm, 1);
//                    mileageMap.put(alarm, Float.valueOf(mileage));
//                }
//            }
//            AtomicInteger totalAlarmCount = new AtomicInteger();
//            final float[] totalAlarmMileage = new float[1];
//            AtomicInteger totalAlarmPerHundred = new AtomicInteger();
//            List<AlarmDetailDto> detailDtoList = alarmTypeMap.keySet().stream().map(s -> {
//                AlarmDetailDto alarmDetailDto = new AlarmDetailDto();
//                alarmDetailDto.setAlarmType(s);
//                alarmDetailDto.setAlarmTypeLabel(dictDetailServicePs.getLabelByValueByCode(s));
//                alarmDetailDto.setAlarmNum(alarmTypeMap.get(s));
//                alarmDetailDto.setAlarmPerHundred((int) (alarmTypeMap.get(s) * 100 / mileageMap.get(s)));
//                totalAlarmCount.set(totalAlarmCount.get() + alarmTypeMap.get(s));
//                totalAlarmMileage[0] = totalAlarmMileage[0] + mileageMap.get(s);
//                totalAlarmPerHundred.set(totalAlarmPerHundred.get() + (int) (alarmTypeMap.get(s) * 100 / mileageMap.get(s)));
//                return alarmDetailDto;
//            }).collect(Collectors.toList());
//            TVehicleAlarmInfoStatisticsResponseDto tVehicleAlarmInfoStatisticsResponseDto = BeanUtil.copyProperties(tVehicleAlarmInfoStatisticsDto, TVehicleAlarmInfoStatisticsResponseDto.class);
//            tVehicleAlarmInfoStatisticsResponseDto.setTotalAlarmNum(totalAlarmCount.get());
//            tVehicleAlarmInfoStatisticsResponseDto.setTotalAlarmPerHundred(totalAlarmPerHundred.get());
//            tVehicleAlarmInfoStatisticsResponseDto.setTotalMileage(totalAlarmMileage[0]);
//            tVehicleAlarmInfoStatisticsResponseDto.setAlarmDetailDtoList(detailDtoList);
//            return tVehicleAlarmInfoStatisticsResponseDto;
//        }).collect(Collectors.toList());
//        return tVehicleAlarmInfoStatisticsResponseDtoList;
//    }

//    /**
//     * 功能描述: 查询未处理的报警信息
//     *
//     * @param tVehicleRealAlarmInfoRequestDto
//     * @return java.util.List<com.quartet.modules.alarm.dto.TVehicleAlarmInfoStatisticsResponseDto>
//     * @author huipanxing huipanxing@chinasofti.com
//     * @date 2021/6/3 11:18
//     */
//    @Override
//    public List<TVehicleSecurityAlarmProcessingResponseDto> selectSecurityAlarmProcessingList(TVehicleRealAlarmInfoRequestDto tVehicleRealAlarmInfoRequestDto) {
//        List<TVehicleSecurityAlarmProcessingResponseDto> tVehicleSecurityAlarmProcessingResponseDtoList = this.baseMapper.selectSecurityAlarmProcessingList(tVehicleRealAlarmInfoRequestDto);
//        Map<String, List<TVehicleSecurityAlarmProcessingResponseDto>> collect = tVehicleSecurityAlarmProcessingResponseDtoList.stream().collect(Collectors.groupingBy(TVehicleSecurityAlarmProcessingResponseDto::getVehicleAlarmType));
//        List<TVehicleSecurityAlarmProcessingResponseDto> tVehicleSecurityAlarmProcessingResponseDtoList1 = collect.keySet().stream().map(k -> {
//            List<TVehicleSecurityAlarmProcessingResponseDto> vehicleSecurityAlarmProcessingResponseDtoList = collect.get(k);
//            TVehicleSecurityAlarmProcessingResponseDto tVehicleSecurityAlarmProcessingResponseDto = vehicleSecurityAlarmProcessingResponseDtoList.get(0);
//            int size = vehicleSecurityAlarmProcessingResponseDtoList.size();
//            tVehicleSecurityAlarmProcessingResponseDto.setAlarmNum(size);
//            tVehicleSecurityAlarmProcessingResponseDto.setFirstAlarmTime(vehicleSecurityAlarmProcessingResponseDtoList.get(0).getAlarmTime());
//            tVehicleSecurityAlarmProcessingResponseDto.setLastAlarmTime(vehicleSecurityAlarmProcessingResponseDtoList.get(size - 1).getAlarmTime());
//            tVehicleSecurityAlarmProcessingResponseDto.setLastPosition(vehicleSecurityAlarmProcessingResponseDtoList.get(size - 1).getPosition());
//            tVehicleSecurityAlarmProcessingResponseDto.setAlarmTypeLabel(dictDetailServicePs.getLabelByValueByCode(tVehicleSecurityAlarmProcessingResponseDto.getAlarmType()));
//            return tVehicleSecurityAlarmProcessingResponseDto;
//        }).collect(Collectors.toList());
//        return tVehicleSecurityAlarmProcessingResponseDtoList1;
//    }
}